/**
 * Copyright 2008 floop.pl
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package org.eaicompiler.database;

import org.eaicompiler.database.model.EventaiScripts;
import org.eaicompiler.database.model.EventaiSummons;
import org.eaicompiler.database.model.EventaiTexts;
import org.eaicompiler.database.model.ScriptLocalizedTexts;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Hibernate database util class
 * 
 * @author floop.pl
 */
public class ScriptdevHibernateUtil {
	private static SessionFactory	sessionFactory		= null;
	private static final int		INDEX_HOST			= 0;
	private static final int		INDEX_PORT			= 1;
	private static final int		INDEX_USER			= 2;
	private static final int		INDEX_PASS			= 3;
	private static final int		INDEX_DATABASE		= 4;

	// Please note, that this connection string is in format of the scriptdev2
	// config file
	private static String			connectionString	= "127.0.0.1;3306;root;root;scriptdev2";

	public static void initialize() {
		try {
			Configuration cfg = new Configuration();
			cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
			cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
			String connectionSplitted[] = connectionString.split(";");
			if (connectionSplitted.length != 5) {
				throw new RuntimeException("Wrong format of scriptdev2 connection string (" + connectionString
						+ "), expected for example 127.0.0.1;3306;root;root;scriptdev2");
			}
			// jdbc:mysql://localhost:3306/database
			cfg.setProperty("hibernate.connection.url", "jdbc:mysql://" + connectionSplitted[INDEX_HOST] + ":"
					+ connectionSplitted[INDEX_PORT] + "/" + connectionSplitted[INDEX_DATABASE]);
			cfg.setProperty("hibernate.connection.username", connectionSplitted[INDEX_USER]);
			cfg.setProperty("hibernate.connection.password", connectionSplitted[INDEX_PASS]);
			cfg.addClass(EventaiScripts.class);
			cfg.addClass(EventaiSummons.class);
			cfg.addClass(ScriptLocalizedTexts.class);
			cfg.addClass(EventaiTexts.class);
			ScriptdevHibernateUtil.sessionFactory = cfg.buildSessionFactory();
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		if(sessionFactory==null)
		{
			initialize();
		}
		return sessionFactory;
	}

	public static void setConnectionString(String connectionString) {
		ScriptdevHibernateUtil.connectionString = connectionString;
	}
}
